import requests, re, multiprocessing
from lxml.html import etree


class SPider:
    def __init__(self):
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        }

    def start_request(self, url):
        response = requests.get(url, headers=self.headers)
        html = response.text
        imgUrl_list = re.findall('data-original="(.*?)"', html)
        imgName_list = re.findall('alt="(.*?)"', html)
        for img_url, img_name in zip(imgUrl_list, imgName_list):
            self.down_img(img_url, img_name)

    def down_img(self, img_url, img_name):
        response = requests.get(img_url, timeout=5)
        print('正在抓取: ' + img_name + '\t' + img_url)
        with open('./python3/' + img_name + '.jpg', 'wb') as f:
            f.write(response.content)
            print('success')

if __name__ == '__main__':
    url = 'http://www.doutula.com/photo/list/?page='
    spider = SPider()
    pool = multiprocessing.Pool()
    spider.start_request(url)
    pool.map(spider.start_request, [url + str(i) for i in range(1, 3)])